package com.hyyt.shopjsp.wxmg.material.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicUsersService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import com.hyyt.shopjsp.wxmg.fans.pojo.WxFansGroup;
import com.hyyt.shopjsp.wxmg.fans.pojo.WxFansUserInfo;
import com.hyyt.shopjsp.wxmg.fans.service.IWxFansGroupService;
import com.hyyt.shopjsp.wxmg.fans.service.IWxFansUserInfoService;
import com.hyyt.shopjsp.wxmg.material.pojo.WxMaterialVideoInfo;
import com.hyyt.shopjsp.wxmg.material.service.IWxMaterialVideoInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WX_素材视频信息 Controller
 *
 * LC
 */
@Controller
@RequestMapping("/back/wx/material/video")
public class WxMaterialVideoInfoController extends BaseController {

    private Logger logger = Logger.getLogger(WxMaterialVideoInfoController.class);
    private String basePath = "/back/wxmg/material/videoInfo/";

    @Resource
    private IWxMaterialVideoInfoService wxMaterialVideoInfoService;
    @Resource
    private IBasicUsersService usersService;
    @Resource
    private IWxFansUserInfoService wxFansUserInfoService;
    @Resource
    private IWxFansGroupService wxFansGroupService;

    /**
     * 进入图文素材信息页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoMaterialVideoPage")
    public String gotoMaterialVideoPage (){
        return basePath + "materialVideoInfoList";
    }

    /**
     * 查询所有图文素材信息列表
     * @param page
     * @param videoTitle            视频标题
     * @param videoName             视频名称
     * @param lastUpdateDateStart   最后修改开始时间
     * @param lastUpdateDateEnd     最后修改结束时间
     * @param deletedFlag           删除标记：0 正常、1 删除
     */
    @RequestMapping(value = "listMaterialVideoInfo",method = RequestMethod.POST)
    @ResponseBody
    public void listMaterialVideoInfo(String videoTitle,String videoName,String lastUpdateDateStart,String lastUpdateDateEnd,String deletedFlag, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        String publicNoInfoStr = RedisDataReadUtil.getRedisValue(StringStaticKey.WX_PUBLIC_NO_INFO);
        Map<String,Object> publicNoInfo = FastJsonUtils.stringToCollect(publicNoInfoStr);
        if(Utils.objectIsNotEmpty(publicNoInfo) && Utils.objectIsNotEmpty(publicNoInfo.get("publicNumberId"))){
            String publicNumberId = String.valueOf(publicNoInfo.get("publicNumberId"));
            param.put("publicNumberId",publicNumberId);
        }
        if (Utils.objectIsNotEmpty(videoTitle)) {
            param.put("videoTitle", videoTitle.trim());
        }
        if (Utils.objectIsNotEmpty(videoName)) {
            param.put("videoName", videoName.trim());
        }
        if(Utils.objectIsNotEmpty(lastUpdateDateStart)){
            param.put("lastUpdateDateStart",lastUpdateDateStart);
        }
        if(Utils.objectIsNotEmpty(lastUpdateDateEnd)){
            param.put("lastUpdateDateEnd",lastUpdateDateEnd);
        }
        if(Utils.objectIsNotEmpty(deletedFlag)){
            param.put("deletedFlag",deletedFlag);
        }
        param.put("sortColumns"," lastUpdateDate desc");
        int totalRecordCount = wxMaterialVideoInfoService.countWxMaterialVideoInfo(param);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<WxMaterialVideoInfo> materialVideoInfoList = wxMaterialVideoInfoService.queryWxMaterialVideoInfo(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",materialVideoInfoList);
        ResultMsg.create(param,response);
    }

    /**
     * 根据id获得图文素材信息对象
     * @param materialVideoInfoId
     * @return
     */
    @RequestMapping(value = "getWxMaterialVideoInfoObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getWxMaterialVideoInfoObject(Integer materialVideoInfoId){
        if (Utils.objectIsNotEmpty(materialVideoInfoId)){
            Map<String,Object> params = new HashMap<>();
            WxMaterialVideoInfo wxMaterialVideoInfo = wxMaterialVideoInfoService.loadWxMaterialVideoInfo(materialVideoInfoId);
            BasicUsers creater = usersService.loadBasicUsers(wxMaterialVideoInfo.getCreater());
            if(Utils.objectIsNotEmpty(creater) && Utils.objectIsNotEmpty(creater.getUserName())){
                params.put("createrName",creater.getUserName());
            }
            BasicUsers modifier = usersService.loadBasicUsers(wxMaterialVideoInfo.getModifier());
            if(Utils.objectIsNotEmpty(modifier) && Utils.objectIsNotEmpty(modifier.getUserName())){
                params.put("modifierName",modifier.getUserName());
            }
            params.put("wxMaterialVideoInfo",wxMaterialVideoInfo);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(params));
        }
        return ResultMsg.create(false);
    }

    /**
     * 根据id还原视频素材信息
     * @param materialVideoInfoId
     * @return
     */
    @RequestMapping(value = "restoreMaterialVideoInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg restoreMaterialVideoInfo(Integer materialVideoInfoId){
        if (Utils.objectIsNotEmpty(materialVideoInfoId)){
            wxMaterialVideoInfoService.restoreWxMaterialVideoInfo(materialVideoInfoId);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据id删除视频素材信息（更改删除标记为 1：删除）
     * @param materialVideoInfoId
     * @return
     */
    @RequestMapping(value = "deleteWxMaterialVideoInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteWxMaterialVideoInfo(Integer materialVideoInfoId){
        if (Utils.objectIsNotEmpty(materialVideoInfoId)){
            WxMaterialVideoInfo wxMaterialVideoInfo = wxMaterialVideoInfoService.loadWxMaterialVideoInfo(materialVideoInfoId);
            BasicUsers basicUsers = getSessionUsers();
            if(Utils.objectIsNotEmpty(wxMaterialVideoInfo)){
                wxMaterialVideoInfo.setModifier(basicUsers.getUsersId());
                wxMaterialVideoInfo.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialVideoInfo.setDeletedBy(basicUsers.getUsersId());
                wxMaterialVideoInfo.setDeletedFlag(1);
                wxMaterialVideoInfo.setDeletionDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialVideoInfoService.updateWxMaterialVideoInfo(wxMaterialVideoInfo);
                return ResultMsg.create(true);
            }
        }else {
            return ResultMsg.create(false);
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或者修改图文素材信息
     * @param wxMaterialVideoInfo
     * @return
     */
    @RequestMapping(value = "saveOrUpdateWxMaterialVideoInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateWxMaterialVideoInfo(WxMaterialVideoInfo wxMaterialVideoInfo){
        if (Utils.objectIsNotEmpty(wxMaterialVideoInfo)){
            BasicUsers basicUsers = getSessionUsers();
            if (Utils.objectIsEmpty(wxMaterialVideoInfo.getMaterialVideoInfoId())){
                wxMaterialVideoInfo.setCreater(basicUsers.getUsersId());
                wxMaterialVideoInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                wxMaterialVideoInfo.setModifier(basicUsers.getUsersId());
                wxMaterialVideoInfo.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialVideoInfoService.saveWxMaterialVideoInfo(wxMaterialVideoInfo);
            }else{
                wxMaterialVideoInfo.setModifier(basicUsers.getUsersId());
                wxMaterialVideoInfo.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialVideoInfoService.updateWxMaterialVideoInfo(wxMaterialVideoInfo);
            }
        }
        return ResultMsg.create(true);
    }

    /**
     * 进入视频素材回收站页面
     * @return
     */
    @RequestMapping("gotoMaterialVideoRecycleBinPage")
    public String gotoMaterialVideoRecycleBinPage (){
        return basePath + "materialVideoRecycleBinList";
    }

    /**
     * 根据id删除视频素材信息（永久删除信息：删除记录）
     * @param materialVideoInfoId
     * @return
     */
    @RequestMapping(value = "deleteMaterialVideoInfoForever",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteMaterialVideoInfoForever(Integer materialVideoInfoId){
        if (Utils.objectIsNotEmpty(materialVideoInfoId)){
            wxMaterialVideoInfoService.deleteWxMaterialVideoInfo(materialVideoInfoId);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 查询所有粉丝用户信息列表
     *
     * @param nickName 会员昵称
     * @param userOpenId 会员标识OPENID
     * @param page 分页
     * @param response 返回结果集
     */
    @RequestMapping(value = "listFansUserInfoInMaterialVideoInfo",method = RequestMethod.POST)
    @ResponseBody
    public void listFansUserInfoInMaterialVideoInfo(String nickName, String userOpenId, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(nickName)) {
            param.put("nickName", nickName.trim());
        }
        if (Utils.objectIsNotEmpty(userOpenId)) {
            param.put("userOpenId", userOpenId.trim());
        }
        int totalRecordCount = wxFansUserInfoService.countWxFansUserInfo(param);
        param.put("sortColumns","userId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<WxFansUserInfo> wxFansUserInfo = wxFansUserInfoService.queryWxFansUserInfo(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",wxFansUserInfo);
        ResultMsg.create(param,response);
    }

    /**
     *查询所有粉丝分组列表
     *
     * @param name 组名称
     * @param page 分页
     * @param response 返回结果集
     */
    @RequestMapping(value = "listFansGroupInMaterialVideoInfo",method = RequestMethod.POST)
    @ResponseBody
    public void listFansGroupInMaterialVideoInfo(String name, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(name)) {
            param.put("name", name.trim());
        }
        int totalRecordCount = wxFansGroupService.countWxFansGroup(param);
        param.put("sortColumns","fansGroupId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<WxFansGroup> wxFansGroup = wxFansGroupService.queryWxFansGroup(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",wxFansGroup);
        ResultMsg.create(param,response);
    }

}
